package edu.nuist.javaProjct.view;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainForm {

    public JPanel mainForm;
    private JButton dataInitBtn;
    private JLabel appStateLabel;
    private JTable myTable;
    private JButton searchBtn;
    private JButton addBtn;
    private JButton deleteBtn;
    private JButton editBtn;

    public MainForm() {

        MyTableModel myTableModel =new MyTableModel();
        myTable.setModel(myTableModel);
        myTable.setVisible(true);

        dataInitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                myTableModel.updateFullData();
                myTable.updateUI();
                appStateLabel.setText("已查询");
            }
        });


        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showChildModel("增加",new AddDataModel().addDataModel);
            }
        });
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showChildModel("删除",new DeleteDataModel().deleteDataModel);
            }
        });
        editBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showChildModel("修改",new EditDataModel().editDataModel);
            }
        });
    }


    private void showChildModel(String title, JPanel openPanel){
        JFrame frame = new JFrame(title);
        frame.setContentPane(openPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setSize(500,500);
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
